package top.ffhome.blog.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.hutool.core.convert.Convert;
import top.ffhome.blog.entity.Article;
import top.ffhome.blog.service.ArticleService;
import top.ffhome.blog.service.LabelService;
import top.ffhome.common.exception.BusinessException;
import top.ffhome.common.page.PageRequest;
import top.ffhome.common.page.PageResult;
import top.ffhome.common.result.Result;
import top.ffhome.system.constant.SysConstants;
import top.ffhome.system.entity.SysDictData;
import top.ffhome.system.service.SysDictDataService;
import top.ffhome.util.core.HtmlUtil;
import top.ffhome.util.core.URLUtil;

@CrossOrigin
@RestController
@RequestMapping("api/index")
public class IndexController {
	@Autowired
	private LabelService labelService;
	@Autowired
	private SysDictDataService dictDataService;
	@Autowired
	private ArticleService articleService;

	@GetMapping(value = "/getLabels")
	public Result getLabels() {
		return Result.success(labelService.selectLabelsByCreateBy(getCreateBy()));
	}

	@PostMapping(value = "/getArticles")
	public Result getArticles(@RequestBody PageRequest request) {
		request.getParams().put("createBy", getCreateBy());
		request.getParams().put("status", SysConstants.NORMAL);
		PageResult<Article> page = articleService.select(request);
		for (Article a : page.getList()) {
			a.setContent(HtmlUtil.filterHtml(a.getContent()));
		}
		return Result.success(page);
	}

	@PostMapping(value = "/getArticle")
	public Result getArticle(@RequestBody Article article) {
		if (article.getId() == null) {
			return Result.fail("参数不正确");
		}
		Article a = articleService.get(article.getId());
		if (!getCreateBy().equals(a.getCreateBy())) {
			return Result.fail("参数超出范围");
		}
		return Result.success(a);
	}

	@PostMapping(value = "/seeArticle")
	public Result seeArticle(@RequestBody Article article) {
		if (article.getId() == null) {
			return Result.fail("参数不正确");
		}
		articleService.addCount(article.getId());
		return Result.success();
	}

	private Long getCreateBy() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		String domain = URLUtil.getDomain(request.getHeader("referer"));
		List<SysDictData> list = dictDataService.selectByCategory(SysDictData.DOMAIN_TO_CREATOR);
		for (SysDictData data : list) {
			if (data.getLabel().equals(domain)) {
				return Convert.toLong(data.getValue());
			}
		}
		throw new BusinessException("域名不在范围内");
	}
}
